<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="<%=request.getContextPath() %>/css/Style.css" type="text/css" rel="stylesheet">
<link href="<%=request.getContextPath() %>/css/jquery-ui-1.7.1.custom.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.3.2.js"></script>
<script type="text/javascript"  src="<%=request.getContextPath() %>/js/jquery-ui-1.7.1.custom.js"></script>
<script type="text/javascript">
	function checkAll(obj) {
		var news = document.getElementsByName("msgs");
		if (obj.checked) {
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = true;
			}
		}else{
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = false;
			}
		}
	}
	function promptDialog(callback, args) {
		if (confirm("是否执行该操作?")) {
			if (args != null) {
				callback(args);
			} else {
				callback();
			}
		}
	}
	
	function deleteItem() {
		var news = document.getElementsByName("msgs");
		var deleteItem = "";
		for ( var i = 0; i < news.length; i++) {
			if(news[i].checked){
				deleteItem+=news[i].value+",";
			}
		}
		if(deleteItem==""){
				alert("至少选择一个才能进行操作！");
				return false;
			}
		var path='<%=request.getContextPath()%>';
		deleteItem = deleteItem.substring(0,deleteItem.length-1);
		var href= window.location.href;
		$.ajax({
			type: "GET",
            url: path + "/deleteMsg.htm",
            data:{
				mid:deleteItem
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                 	 alert("操作成功！");
                	 window.location.href=href;
                 }else{
              	   alert("操作失败！");
                 }
            },
            error: function(){
              alert("数据库繁忙，请稍后再试！");
            }
		});
	}
	function goPage(page){
			document.getElementById("currentPage").value=page;
			document.getElementById("n_form").submit();
		}
	
	function deleteAdmin(_id){
		var path='<%=request.getContextPath()%>';
		$.ajax({
			type: "GET",
            url: path + "/deleteAdmin.htm",
            data:{
				aid:_id
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                 	 alert("操作成功！");
                	 //刷新页面
                 	document.getElementById("n_form").submit();
                 }else{
              	  	 alert("操作失败！");
                 }
            },
            error: function(){
              alert("服务器繁忙，请稍后再试！");
            }
		});
	}
</script>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" class="thead" width="100%">
	<tr><td style="padding:3px;">&nbsp;</td></tr>
</table>
<form action="<%=request.getContextPath() %>/adminList.htm" method="post" id="n_form">
<table cellspacing="0" cellpadding="0" bordercolor="#003366" border="1" width="100%" class="Table1">
	<tbody>
		<tr height="30px" >
			<td colspan="4" >
				<span class="top_c"><input type="button" value="添加" onclick="showEditAdminDialog('','')" class="btn"/></span>
			</td>
		</tr>
		<tr>
			<td height="25" align="center" style="display:none;"><input type="checkbox" onclick="checkAll(this)" /></td>
			<td height="25" align="center" >帐号</td>
			<td height="25" align="center" >添加时间</td>
			<td height="25" align="center" >登录次数</td>
			<td height="25" align="center" >操作</td>
		</tr>
		<s:iterator value="admins" id="item" status="index">
		<tr>
			<td height="25" align="center" style="display:none;"><input type="checkbox" name="msgs" value="<s:property value="#item.id"/>"/></td>
			<td height="25" align="center"><s:property value="#item.username"/></td>
			<td height="25" align="center"><s:date name="#item.registTime" nice="false" format="yyyy-MM-dd HH:mm:ss"/></td>
			<td height="25" align="center"><s:property value="#item.loginCount"/>次</td>
			<td height="25" align="center">
				[<a style="color: red;" href="javascript:void();" onclick="showEditAdminDialog('<s:property value="#item.id"/>','<s:property value="#item.username"/>');">编辑</a>]
				| 
				[<a style="color: red;" href="javascript:void();" onclick="promptDialog(deleteAdmin,'<s:property value="#item.id"/>');">删除</a>]
			</td>
		</tr>
		</s:iterator>
	</tbody>
</table>
</form>
<div id="edit_dialog" title="" style="display: none">
	<table cellpadding="2" cellspacing="1" border="0" width="100%" align="center">
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td width="30%" align="right" class='td1'>帐号：<font color='#FF0000'>*</font>：</td>
		<td width="70%" class='td1' align="center">
			<input name='username' type='text'  id="username" size="20"/>
		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr align="center">
		<td width="30%" align="right" class='td1' >密码：<font color='#FF0000'>*</font>：</td>
		<td width="70%" class='td1'>
			<input name='password' type='password' id="password" size="20"/>
		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr align="center">
		<td width="30%" align="right" class='td1' >确认密码：<font color='#FF0000'>*</font>：</td>
		<td width="70%" class='td1'>
			<input name='repassword' type='password' id="repassword" size="20"/>
		</td>
	</tr>
	</table>
	<input type="hidden" value="" id="aid" name="aid">
</div>
<script type="text/javascript">
var dia_img = $("#edit_dialog").dialog( {
	bgiframe :true,
	resizable :false,
	autoOpen :false,
	modal :true,
	draggable:false,
	overlay : {
		backgroundColor :'#000',
		opacity :0.5
	},
	height : 250,
	width : 400,
	buttons : {
		"确定":function(){
			//检查时间是否正确
			var _username = $("#username").val();
			var _password = $("#password").val();
			var _repassword = $("#repassword").val();
			if(_username == null || _username.length<1){
				alert("请输入帐号");
				return;
			}
			if(_password == null || _password.length<1){
				alert("请输入密码");
				return;
			}
			if(_repassword == null || _repassword.length<1){
				alert("请输入确定密码");
				return;
			}
			if(_password!=_repassword){
				alert("两次密码输入不一致");
				return;
			}
			var _aid = $("#aid").val();
			var path='<%=request.getContextPath()%>';
			$.ajax({
				type: "POST",
	            url: path + "/editAdmin.htm",
	            data:{
	            	aid:_aid,
	            	username:_username,
	            	password:_password
	            },
	            dataType: "text",
	            success: function(result){
	            	 if(result=="success"){
	                 	alert("操作成功！");
	                 	dia_img.dialog('close');
	                 	document.getElementById("n_form").submit();
	                 }else if(result == "exist"){
	              	    alert("管理员帐号已经存在,操作失败!");
	                 }else{
	                	 alert("操作失败！"); 
	                 }
	            },
	            error: function(){
	              alert("服务器繁忙，请稍后再试！");
	            }
			});
		},
		"取消":function(){
			dia_img.dialog('close');
		}
	}
});
function showEditAdminDialog(id,_username){
	//新增用户
	if(_username != null && _username.length>0){
		$("#aid").val(id);
		$("#username").val(_username);
		$("#username").attr("disabled","disabled");
	}
	dia_img.dialog('open');
}
</script>
</body>
</html>